package com.contact;

import java.util.HashMap;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.VideoView;

import com.app.DemoApplication;
import com.common.method.CommonData;
import com.http.NetCallBack;
import com.http.UserVideoAddTask;
import com.matang.R;
import com.modle.User;
import com.util.UserUtil;

public class VideoPlayerUserActivity extends BaseActivity implements
		MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener,
		OnPreparedListener {
	String tag = "VideoPlayerUserActivity";
	ImageView thumbIv;
	VideoView mPlayer;
	PBtmControllerHolder portBtmHolder;// 竖屏底部
	TextView descTv, nickNameTv;
	Button videoBtn, okBtn;
	UserVAudio uvData;


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_videoplay_user);
		init();
	}

	private void init(){
		mContext = VideoPlayerUserActivity.this;
		findViews();
		initPaneData();
	}

	@Override
	protected void findViews() {
		super.findViews();
	
		videoBtn = (Button) findViewById(R.id.v_play_btn);
		okBtn = (Button) findViewById(R.id.ok_btn);
		thumbIv = (ImageView) findViewById(R.id.thumb_iv);
		mPlayer = (VideoView) findViewById(R.id.videoview);
		descTv = (TextView) findViewById(R.id.desc_tv);
		nickNameTv = (TextView) findViewById(R.id.nickname_tv);

	

	}

	protected void initPaneData() {
		super.initPaneData();
		header.headTitleTv.setText("个人视频");
		uvData = (UserVAudio) getIntent().getSerializableExtra("uvData");


		portBtmHolder = new PBtmControllerHolder();
		portBtmHolder.container = (RelativeLayout) findViewById(R.id.p_btm_pane_container);
		portBtmHolder.totalTimeTv = (TextView) findViewById(R.id.p_total_time);
		portBtmHolder.currentTimeTv = (TextView) findViewById(R.id.p_current_time);
		portBtmHolder.videoSeekBar = (SeekBar) findViewById(R.id.p_seekbar);
		portBtmHolder.videoSeekBar.setMax(1000);
		portBtmHolder.videoSeekBar
				.setOnSeekBarChangeListener(pSeekVideoListener);
		// 触摸底部菜单关闭底部菜单
		portBtmHolder.container.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
		mPlayer.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View view, MotionEvent evt) {
				if (MotionEvent.ACTION_DOWN == evt.getAction()) {
					if (mShowing) {
						hideBar();
					} else {
						showBar();
					}

				}

				return true;
			}
		});
		mPlayer.setOnCompletionListener(this);
		mPlayer.setOnPreparedListener(this);
		mPlayer.setOnErrorListener(this);
		videoBtn.setOnClickListener(click);

	
//		if(!SpUserUtil.getIsDistrib()){
		play(uvData.videoUrl);
//		}

	
	}

	private void doPlayClick() {
		if (ObjTool.isNotNull(uvData.videoUrl)) {
			play(uvData.videoUrl);
			mPlayer.setVisibility(View.VISIBLE);
			videoBtn.setVisibility(View.GONE);

		}
	}

	OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ok_btn:
//				// 保存视频
//				HashMap<String, Object> map = new HashMap<String, Object>();
//				map.put("User", UserUtil.getUserID());
//				map.put("url", uvData.videoUrl);
//				map.put("thumbnailsUrl", uvData.videoUrl);
//				map.put("type", "video");
//				map.put("description", uvData.desc);
//				addVideoToServer(map);
				break;
			case R.id.v_play_btn:
				doPlayClick();
				break;

			}
		}
	};

	/*********************************************************************/

	private boolean mDragging;// 判断是否正在拖动
	private static final int SHOW_PROGRESS = 990;// 用于更新播放进度条的事件标志
	private static final int HIDE_PROGRESS_P = 991;// 用于定时隐藏bar
	boolean mShowing = false;// 判断是否已经显示

	public Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			dismissProgressDialog();
			switch (msg.what) {
			case SHOW_PROGRESS: {

				setProgress();

				if (!mDragging && mShowing && mPlayer.isPlaying()) {
					msg = obtainMessage(SHOW_PROGRESS);
					handler.sendMessageDelayed(msg, 1000);
				}
				break;
			}

			case HIDE_PROGRESS_P: {
				hideBar();

			}

			}
		}

	};

	void play(String videoPath) {
	
		if (ObjTool.isNotNull(videoPath)) {
			Uri uri = Uri.parse(videoPath);
			mPlayer.setVideoURI(uri);
			mPlayer.requestFocus();
		}
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
	
		videoBtn.setVisibility(View.GONE);

		mPlayer.start();
		showBar();

	}

	@Override
	public boolean onError(MediaPlayer m, int whatError, int extra) {
		switch (whatError) {
		case MediaPlayer.MEDIA_ERROR_SERVER_DIED: {
			AppTool.tsMsg(mContext, "播放视频失败,错误码(" + whatError + "," + extra
					+ ")");
		

			break;
		}
		case MediaPlayer.MEDIA_ERROR_UNKNOWN: {

			AppTool.tsMsg(mContext, "播放视频失败,错误码(" + whatError + "," + extra
					+ ")");

		
			break;
		}
		default:
			AppTool.tsMsg(mContext, "播放视频失败,错误码(" + whatError + "," + extra
					+ ")");
			
			break;
		}
		return true;
	}

	public void onCompletion(MediaPlayer mp) {
		videoBtn.setVisibility(View.VISIBLE);

	}

	private OnSeekBarChangeListener pSeekVideoListener = new OnSeekBarChangeListener() {
		public void onStartTrackingTouch(SeekBar bar) {
			mDragging = true;
			handler.removeMessages(SHOW_PROGRESS);
			handler.removeMessages(HIDE_PROGRESS_P);
		}

		public void onProgressChanged(SeekBar bar, int progress,
				boolean fromuser) {

			long duration = mPlayer.getDuration();
			long newposition = (duration * progress) / 1000L;

			if (portBtmHolder.currentTimeTv != null)
				portBtmHolder.currentTimeTv.setText(StringTool
						.stringForTime(newposition));

		}

		public void onStopTrackingTouch(SeekBar bar) {
			long duration = mPlayer.getDuration();
			long newposition = (duration * bar.getProgress()) / 1000L;
			mPlayer.seekTo((int) newposition);
			mDragging = false;
			handler.sendEmptyMessage(SHOW_PROGRESS);
			handler.sendEmptyMessageDelayed(HIDE_PROGRESS_P,
					CommonData.BAR_SHOW_TIME);
		}
	};

	private long setProgress() {
		if (mPlayer == null || mDragging) {
			return 0;
		}
		long position = mPlayer.getCurrentPosition();
		long duration = mPlayer.getDuration();
		int bufferPercentage = mPlayer.getBufferPercentage();

		// 竖屏
		if (portBtmHolder.videoSeekBar != null) {
			if (duration > 0) {
				long pos = 1000L * position / duration;
				portBtmHolder.videoSeekBar.setProgress((int) pos);
			}
			if (bufferPercentage > 0) {
				portBtmHolder.videoSeekBar
						.setSecondaryProgress(bufferPercentage * 10);
			}
		}

		if (portBtmHolder.totalTimeTv != null)
			portBtmHolder.totalTimeTv.setText(StringTool
					.stringForTime(duration));
		if (portBtmHolder.currentTimeTv != null)
			portBtmHolder.currentTimeTv.setText(StringTool
					.stringForTime(position));

		return position;
	}

	private void hideBar() {
		mShowing = false;
		handler.removeMessages(SHOW_PROGRESS);
		portBtmHolder.container.setVisibility(View.GONE);
	}

	/**
	 * 
	 * @Description:TODO显示控制栏
	 * @return void
	 * @author zhuw
	 * @date 2013-4-27 上午10:10:32
	 */
	private void showBar() {

		mShowing = true;
		portBtmHolder.container.setVisibility(View.VISIBLE);

		handler.sendEmptyMessage(SHOW_PROGRESS);
		handler.removeMessages(HIDE_PROGRESS_P);
		handler.sendEmptyMessageDelayed(HIDE_PROGRESS_P,
				CommonData.BAR_SHOW_TIME);

	}

	/**
	 * 
	 * @ClassName:PController竖屏底部控制栏ViewHoler
	 * @Description:TODO
	 * @author zhuw
	 * @date 2013-4-11 下午2:48:01
	 */
	class PBtmControllerHolder {
		TextView totalTimeTv, currentTimeTv;
		SeekBar videoSeekBar;
		RelativeLayout container;
	}

	/**
	 * 
	 * @Description:TODO上传后更新图片路径信息到server
	 * @param@param map
	 * @return void
	 * @author zhuw
	 * @date 2013-8-17 下午1:12:49
	 */
	
}
